﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using IHISCETeim.Library;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using IHISCETeim.Model;
using System.Configuration;
namespace IHISCETeim.DAL
{
    public  class SMSProccessDAL
    {
        /// <summary>
        /// 添加短信需处理记录
        /// </summary>
        /// <param name="SMSSouce"></param>
        /// <param name="ObjId"></param>
        /// <param name="SMSTempleteKeyOrContent"></param>
        /// <returns></returns>
        public static int Add(string SMSSouce, string ObjId, string SMSTempleteKeyOrContent)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("SMSProccess_Add", SMSSouce, ObjId, SMSTempleteKeyOrContent);
            return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
        }
        /// <summary>
        /// 获取所有未处理记录
        /// </summary>
        /// <returns></returns>
        public static DataTable GetNeedPorccessAll()
	    {
		    Database DB = DatabaseFactory.CreateDatabase();		
		
            DbCommand dbCommand = DB.GetStoredProcCommand("SMSProccess_GetNeedPorccessAll");
            return DB.ExecuteDataSet(dbCommand).Tables[0];
	    }
        /// <summary>
        /// 更新已处理
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public static bool Update(int ID)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("SMSProccess_Update", ID);
            try
            {
                return Convert.ToInt32(DB.ExecuteScalar(dbCommand)) > 0;
            }
            catch (Exception exc)
            {
                return false;
            }
        }

        /// <summary>
        /// 根据来源获取需处理的内容,可用于处理一天只处理一次的情况,只需判断记录存不存在
        /// </summary>
        /// <returns></returns>
        public static DataTable GetBySMSSouce(string SMSSouce)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("SMSProccess_GetBySMSSouce", SMSSouce);
            return DB.ExecuteDataSet(dbCommand).Tables[0];
        }

        /// <summary>
        /// 获取短信发送列表
        /// </summary>
        /// <param name="whereSql"></param>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <param name="Sort"></param>
        /// <param name="TotalCount"></param>
        /// <returns></returns>
        public static DataTable SMSList_GetPagination(string whereSql, int PageIndex, int PageSize, string Sort, ref int TotalCount)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("SMSList_GetPagination");
            DB.AddInParameter(dbCommand, "whereSql", DbType.String, whereSql);
            DB.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            DB.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            DB.AddInParameter(dbCommand, "Sort", DbType.String, Sort);
            DB.AddOutParameter(dbCommand, "TotalCount", DbType.Int32, 8);
            DataTable dt = DB.ExecuteDataSet(dbCommand).Tables[0];

            TotalCount = Convert.ToInt32(DB.GetParameterValue(dbCommand, "TotalCount"));
            return dt;
        }
    }
}
